package com.example.shoppingdemo.nslcommodity.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.shoppingdemo.R;
import com.example.shoppingdemo.nslcommodity.IResult.CommodityID;
import com.example.shoppingdemo.nslcommodity.IResult.ICommodity;
import com.example.shoppingdemo.nslcommodity.OrderForm;
import com.example.shoppingdemo.nslcommodity.javabean.AdapterModel;
import com.example.shoppingdemo.nslcommodity.adapter.EstimateAdapter;
import com.example.shoppingdemo.nslcommodity.javabean.EstModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 已完成的Fragment
 */
public class EstimateFragment extends Fragment {
    private RecyclerView estimate;
    private List<EstModel> adapterModelList;
    private EstimateAdapter adapter;
    private ICommodity commodityResult;
    LinearLayout allLayout;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_estimate, container, false);
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        commodityResult=new CommodityID();
        estimate = view.findViewById(R.id.order_form_estimate);
        allLayout=view.findViewById(R.id.layout_order_form_estimate);
        initView();
    }
    private void initView(){
        estimate.setLayoutManager(new LinearLayoutManager(requireActivity()));
        adapterModelList = new ArrayList<>();
        adapter = new EstimateAdapter(requireActivity());
        adapter.setAdapterModels(adapterModelList);
        adapter.setListenter(new EstimateAdapter.onEstimateParticulars() {
            @Override
            public void onEstimateResult(int position) {
                Intent intent=new Intent(requireActivity(), OrderForm.class);
                intent.putExtra("id",adapterModelList.get(position).getId());
                startActivity(intent);
            }
        });
        estimate.setAdapter(adapter);
        commodityResult.onEstlistresult(1,new CommodityID.onEstlistsResponse() {
            @Override
            public void onEstlist(List<EstModel> Estlists) {
                adapterModelList.clear();
                adapterModelList.addAll(Estlists);
                if (Estlists.size()==0){
                    allLayout.setVisibility(View.VISIBLE);
                }else {
                    adapter.setAdapterModels(adapterModelList);
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }
}